//
//  CJSSearchCell.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/30.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSSearchCell.h"
#import "UIImageView+WebCache.h"
#import "GSYImageView.h"

@interface CJSSearchCell ()

@property (nonatomic, retain) UIView *superView;

@property (nonatomic, retain) UIImageView *picImage;

@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UILabel *timeLabel;

@property (nonatomic, retain) UILabel *dayLabel;

@property (nonatomic, retain) UILabel *personLabel;

@end

@implementation CJSSearchCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    
    self.superView = [[UIView alloc] initWithFrame:CGRectMake(10 * ADAPT_WIDTH, 5, SCREEN_WIDTH - 20 * ADAPT_WIDTH, 150 * ADAPT_HEIGHT)];
    self.superView.layer.cornerRadius = 5;
    self.superView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.superView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.superView.bounds];
    [imageView setImage:[UIImage imageNamed:@"poi_bg_placeholder@2x.png"]];
    [self.superView addSubview:imageView];
    
    CGRect frame = self.superView.bounds;
    frame.size.width = frame.size.width * 2 / 5;
    UIImageView *blackImage = [[UIImageView alloc] initWithFrame:frame];
    [blackImage setImage:[UIImage imageNamed:@"trips_cover_mask@2x.png"]];
    [self.superView addSubview:blackImage];
    
    GSYImageView *occupyImage = [[GSYImageView alloc] initWithFrame:self.superView.bounds ];
    [self.superView addSubview:occupyImage];
    
    self.picImage = [[UIImageView alloc] initWithFrame:self.superView.bounds];
    self.picImage.contentMode = UIViewContentModeScaleAspectFill;
    [self.superView addSubview:self.picImage];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * ADAPT_WIDTH, 10 * ADAPT_HEIGHT, self.superView.frame.size.width - 40 * ADAPT_WIDTH, 35 * ADAPT_HEIGHT)];
    self.titleLabel.font = [UIFont systemFontOfSize:22 weight:1.3];
    self.titleLabel.shadowColor = [UIColor grayColor];
    self.titleLabel.shadowOffset = CGSizeMake(1, 1);
    self.titleLabel.textColor = [UIColor whiteColor];
    [self.superView addSubview:self.titleLabel];
    
    UIImageView *aview = [[UIImageView alloc] initWithFrame:CGRectMake(22 * ADAPT_WIDTH, self.titleLabel.frame.size.height + 10 * ADAPT_HEIGHT, 4 * ADAPT_WIDTH, 15 * ADAPT_HEIGHT)];
    aview.layer.cornerRadius = 2;
    aview.layer.masksToBounds = YES;
    aview.image = [UIImage imageNamed:@"login_btn.png"];
    [self.superView addSubview:aview];
    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(33 * ADAPT_WIDTH, self.titleLabel.frame.size.height + 10 * ADAPT_HEIGHT, 80 * ADAPT_WIDTH, 15 * ADAPT_HEIGHT)];
    self.timeLabel.font = [UIFont systemFontOfSize:12 weight:1];
    self.timeLabel.shadowColor = [UIColor grayColor];
    self.timeLabel.shadowOffset = CGSizeMake(1, 1);
    self.timeLabel.textColor = [UIColor whiteColor];
    [self.superView addSubview:self.timeLabel];
    
    self.dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.timeLabel.frame.size.width + 35 * ADAPT_WIDTH, self.titleLabel.frame.size.height + 10 * ADAPT_HEIGHT, 100 * ADAPT_WIDTH, 15 * ADAPT_HEIGHT)];
    self.dayLabel.font = [UIFont systemFontOfSize:12 weight:1];
    self.dayLabel.shadowColor = [UIColor grayColor];
    self.dayLabel.shadowOffset = CGSizeMake(1, 1);
    self.dayLabel.textColor = [UIColor whiteColor];
    [self.superView addSubview:self.dayLabel];
    
    self.personLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 * ADAPT_WIDTH, self.superView.frame.size.height - 30 * ADAPT_HEIGHT, 330 * ADAPT_WIDTH, 15 * ADAPT_HEIGHT)];
    self.personLabel.font = [UIFont systemFontOfSize:12 weight:1];
    self.personLabel.shadowColor = [UIColor grayColor];
    self.personLabel.shadowOffset = CGSizeMake(1, 1);
    self.personLabel.textColor = [UIColor whiteColor];
    [self.superView addSubview:self.personLabel];
    
}

- (void)setSearch:(CJSSearch *)search {
    
    _search = search;
    
    [self.picImage sd_setImageWithURL:[NSURL URLWithString:self.search.cover_image_default]];
    
    NSString *title = self.search.name;
    NSString *str = self.lightString; //是需要变高亮的文字
    
    // 设置标签文字
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:title];
    
    // 获取标红的位置和长度
    NSRange range = [title rangeOfString:str]; //获取需要变量文字的文字
    
    // 设置标签文字的属性
    [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:23]} range:range];
    // 显示在Label上
    self.titleLabel.attributedText = attrituteString;
    self.timeLabel.text = self.search.date;
    self.dayLabel.text = [NSString stringWithFormat:@"%@ 足迹", self.search.waypoints];
    self.personLabel.text = [NSString stringWithFormat:@"%@ 浏览 / %@ 喜欢 / %@ 评论", self.search.view_count, self.search.liked_count, self.search.waypoints];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
